#ifndef RANDOM_GENERATOR_H_
#define RANDOM_GENERATOR_H_

#include "../Common/GlobalDefines.h"
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <vector>
#include <algorithm>
#include "../Math/vectorN.h"

GLOBAL_NAMESPACE_BEGIN

namespace Random_Generator
{
	static double PI = 3.14159265;

	static void Start()
	{
		srand(unsigned int(time(NULL)));
	}

    static double sampleNormal() 
    {
        double u = ((double) rand() / (RAND_MAX)) * 2 - 1;
        double v = ((double) rand() / (RAND_MAX)) * 2 - 1;
        double r = u * u + v * v;
        if (r == 0 || r > 1) return sampleNormal();
        double c = sqrt(-2 * log(r) / r);
        return u * c;
    }

	static float getRandom()
	{
		float ret = (float)rand()/RAND_MAX;
		return ret;
	}

    static int getRandom(int minValue, int maxValue)
    {
        return rand()%(maxValue-minValue)+minValue;
    }

    static std::vector<double> randomNormalVector(int nSamples, double miu)
    {
        //Start();
        std::vector<double> ret;
        for (int i = 0; i < nSamples; i++)
        {
            ret.push_back(sampleNormal());
        }
        double maxF = *std::max_element(ret.begin(), ret.end());
        double minF = *std::min_element(ret.begin(), ret.end());

        for (int i = 0; i < nSamples; i++)
        {
            ret[i] = (ret[i]-minF)/(maxF-minF)+miu;
        }
        return ret;
    }

    static std::vector<MATH::vector2d> randomTest()
    {
        int nSample = 1000;
        std::vector<double> x, y;
        std::vector<double> tmp;

        Start();
        for (int i = 0; i <= 5; i++)
        {
            tmp = randomNormalVector(nSample, 2.0*i/5);
            x.insert(x.end(), tmp.begin(), tmp.end());
            tmp = randomNormalVector(nSample, 2.0*i/5);
            y.insert(y.end(), tmp.begin(), tmp.end());
        }

        std::vector<MATH::vector2d> ret;
        for (int i = 0; i < x.size(); i++)
        {
            ret.push_back(MATH::vector2d(x[i], y[i]));
        }
        return ret;
    }
}

GLOBAL_NAMESPACE_END

#endif//RANDOM_GENERATOR_H_